<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>API 参考 | Vue Router</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="static/picture/logo.png">
    <link rel="apple-touch-icon" href="https://v3.router.vuejs.org/icons/apple-touch-icon-152x152.png">
    <link rel="mask-icon" href="https://v3.router.vuejs.org/icons/safari-pinned-tab.svg" color="#3eaf7c">
    <meta name="description" content="Vue.js 官方的路由管理器。">
    <meta name="wwads-cn-verify" content="7e7757b1e12abcb736ab9a754ffb617a">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    
    <link rel="preload" href="static/css/0.styles.136ede33.css" as="style"><link rel="preload" href="https://v3.router.vuejs.org/assets/js/app.5cb068d1.js" as="script"><link rel="preload" href="https://v3.router.vuejs.org/assets/js/4.1430b5a5.js" as="script"><link rel="preload" href="https://v3.router.vuejs.org/assets/js/3.51f41756.js" as="script"><link rel="preload" href="https://v3.router.vuejs.org/assets/js/107.45f15a23.js" as="script"><link rel="preload" href="https://v3.router.vuejs.org/assets/js/7.b1ef01fe.js" as="script"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/10.31570374.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/100.f5a92b3f.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/101.ee7125f5.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/102.d6663468.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/103.52eae1a0.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/104.c3f7f210.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/105.eeea8ced.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/106.1c26feed.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/108.67b3498d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/109.0d861fad.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/11.23ce75fe.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/110.817be425.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/111.1ff37cf8.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/112.0f4bcc2b.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/113.33f197d8.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/114.59d11efc.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/115.e29513c5.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/116.d3be25ed.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/117.520112d7.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/118.4e8860bf.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/119.822915fe.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/12.f9f5c1d4.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/120.64de5730.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/121.d14483d6.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/122.809b1d6c.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/123.dfafa172.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/124.e1cdb337.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/125.093b479c.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/13.db1acc91.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/14.17c88755.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/15.6700843a.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/16.84949dd1.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/17.8f87f2ef.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/18.90f9825b.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/19.196286e9.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/20.8a2eefe0.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/21.9e49382e.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/22.0bdc0b6f.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/23.83b61690.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/24.445c95b4.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/25.40493d84.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/26.2a95a52d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/27.1856a4c3.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/28.9f6e959e.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/29.06df98c9.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/30.be9a3758.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/31.2751df13.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/32.3df6388b.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/33.73d91d13.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/34.42fa6d43.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/35.92c4dbe0.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/36.3cb64f0d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/37.d00b8ad5.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/38.8cfdc40e.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/39.663f424b.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/40.d5721761.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/41.0dd61abe.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/42.d09ca009.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/43.539e190a.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/44.7907e586.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/45.273a044f.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/46.8680fbf0.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/47.f68a4eb9.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/48.3616928b.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/49.bf7cdfd5.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/5.2d9bb619.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/50.ec7d6bdf.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/51.6c2151bf.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/52.b853c8bc.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/53.14dd74fd.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/54.63232eeb.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/55.9582f69a.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/56.4b1cd6ef.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/57.a027b996.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/58.07b675b0.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/59.bb8da942.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/6.02bc56cd.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/60.67d68613.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/61.f5eb4a0d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/62.d0da6098.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/63.c3bfaec4.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/64.56511c8c.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/65.b0d60684.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/66.e066bc4d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/67.fdc123f4.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/68.b78eaac4.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/69.ab78abab.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/70.6ca6222a.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/71.442171e5.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/72.12cba91d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/73.258ad65e.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/74.42a25693.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/75.dcf907be.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/76.39d8dfc2.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/77.ef3ec9f9.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/78.4c82cfbf.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/79.b5d92855.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/8.dee4ab51.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/80.d55e87d6.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/81.cf409eab.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/82.fd3a33a6.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/83.0e5cf847.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/84.28b2a6b8.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/85.3a729073.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/86.c985b80b.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/87.62964e61.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/88.ceb8b07d.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/89.2c0b86ae.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/9.a79fac11.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/90.b80f9f69.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/91.ecc4a957.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/92.2165392e.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/93.de463e4e.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/94.b595fcb4.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/95.5eb09d5f.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/96.221390cd.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/97.4daa1cf5.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/98.860a2ac1.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/99.33c3adae.js"><link rel="prefetch" href="https://v3.router.vuejs.org/assets/js/vendors~docsearch.2dc030f4.js">
    <link rel="stylesheet" href="static/css/0.styles.136ede33.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="main-container" data-v-84121532=""><div class="theme-container" data-v-84121532=""><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewbox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="index4.html" class="home-link router-link-active"><!----> <span class="site-name">Vue Router</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"><div class="nav-item"><a href="index10.html" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  API 参考
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="v3.x" class="dropdown-title"><span class="title">v3.x</span> <span class="arrow down"></span></button> <button type="button" aria-label="v3.x" class="mobile-dropdown-title"><span class="title">v3.x</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="index1.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  v4.x
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">选择语言</span> <span class="arrow down"></span></button> <button type="button" aria-label="Select language" class="mobile-dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  English
</a></li><li class="dropdown-item"><!----> <a href="" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  简体中文
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  日本語
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  Русский
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  한국어
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  Français
</a></li></ul></div></div> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="index10.html" class="nav-link">
  指南
</a></div><div class="nav-item"><a href="" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  API 参考
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="v3.x" class="dropdown-title"><span class="title">v3.x</span> <span class="arrow down"></span></button> <button type="button" aria-label="v3.x" class="mobile-dropdown-title"><span class="title">v3.x</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="index1.html" target="_blank" rel="noopener noreferrer" class="nav-link external">
  v4.x
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div><div class="nav-item"><a href="javascript:;" target="_blank" rel="noopener noreferrer" class="nav-link external">
  更新记录
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Select language" class="dropdown-title"><span class="title">选择语言</span> <span class="arrow down"></span></button> <button type="button" aria-label="Select language" class="mobile-dropdown-title"><span class="title">选择语言</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  English
</a></li><li class="dropdown-item"><!----> <a href="" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  简体中文
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  日本語
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  Русский
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  한국어
</a></li><li class="dropdown-item"><!----> <a href="javascript:;" class="nav-link">
  Français
</a></li></ul></div></div> <a href="javascript:;" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <div class="sponsors sponsors-top" data-v-84121532=""><span data-v-84121532="">Platinum Sponsors</span> <a href="javascript:;" target="_blank" rel="noopener" data-v-84121532=""><img src="https://posva-sponsors.pages.dev/logos/fincliplogo_black_svg.svg" alt="Finogeeks" data-v-84121532=""></a></div> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>API 参考</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="#router-link" class="sidebar-link">&lt;router-link&gt;</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#v-slot-api-3-1-0-新增" class="sidebar-link">v-slot API (3.1.0 新增)</a></li><li class="sidebar-sub-header"><a href="#示例-将激活的-class-应用在外层元素" class="sidebar-link">示例：将激活的 class 应用在外层元素</a></li></ul></li><li><a href="#router-link-props" class="sidebar-link">&lt;router-link&gt; Props</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#to" class="sidebar-link">to</a></li><li class="sidebar-sub-header"><a href="#replace" class="sidebar-link">replace</a></li><li class="sidebar-sub-header"><a href="#append" class="sidebar-link">append</a></li><li class="sidebar-sub-header"><a href="#tag" class="sidebar-link">tag</a></li><li class="sidebar-sub-header"><a href="#active-class" class="sidebar-link">active-class</a></li><li class="sidebar-sub-header"><a href="#exact" class="sidebar-link">exact</a></li><li class="sidebar-sub-header"><a href="#event" class="sidebar-link">event</a></li><li class="sidebar-sub-header"><a href="#exact-active-class" class="sidebar-link">exact-active-class</a></li><li class="sidebar-sub-header"><a href="#aria-current-value" class="sidebar-link">aria-current-value</a></li></ul></li><li><a href="#router-view" class="sidebar-link">&lt;router-view&gt;</a><ul class="sidebar-sub-headers"></ul></li><li><a href="#router-view-props" class="sidebar-link">&lt;router-view&gt; Props</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#name" class="sidebar-link">name</a></li></ul></li><li><a href="#router-构建选项" class="sidebar-link">Router 构建选项</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#routes" class="sidebar-link">routes</a></li><li class="sidebar-sub-header"><a href="#mode" class="sidebar-link">mode</a></li><li class="sidebar-sub-header"><a href="#base" class="sidebar-link">base</a></li><li class="sidebar-sub-header"><a href="#linkactiveclass" class="sidebar-link">linkActiveClass</a></li><li class="sidebar-sub-header"><a href="#linkexactactiveclass" class="sidebar-link">linkExactActiveClass</a></li><li class="sidebar-sub-header"><a href="#scrollbehavior" class="sidebar-link">scrollBehavior</a></li><li class="sidebar-sub-header"><a href="#parsequery-stringifyquery" class="sidebar-link">parseQuery / stringifyQuery</a></li><li class="sidebar-sub-header"><a href="#fallback" class="sidebar-link">fallback</a></li></ul></li><li><a href="#router-实例属性" class="sidebar-link">Router 实例属性</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#router-app" class="sidebar-link">router.app</a></li><li class="sidebar-sub-header"><a href="#router-mode" class="sidebar-link">router.mode</a></li><li class="sidebar-sub-header"><a href="#router-currentroute" class="sidebar-link">router.currentRoute</a></li><li class="sidebar-sub-header"><a href="#router-start-location" class="sidebar-link">router.START_LOCATION</a></li></ul></li><li><a href="#router-实例方法" class="sidebar-link">Router 实例方法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#router-beforeeach" class="sidebar-link">router.beforeEach</a></li><li class="sidebar-sub-header"><a href="#router-beforeresolve" class="sidebar-link">router.beforeResolve</a></li><li class="sidebar-sub-header"><a href="#router-aftereach" class="sidebar-link">router.afterEach</a></li><li class="sidebar-sub-header"><a href="#router-push" class="sidebar-link">router.push</a></li><li class="sidebar-sub-header"><a href="#router-replace" class="sidebar-link">router.replace</a></li><li class="sidebar-sub-header"><a href="#router-go" class="sidebar-link">router.go</a></li><li class="sidebar-sub-header"><a href="#router-back" class="sidebar-link">router.back</a></li><li class="sidebar-sub-header"><a href="#router-forward" class="sidebar-link">router.forward</a></li><li class="sidebar-sub-header"><a href="#router-getmatchedcomponents" class="sidebar-link">router.getMatchedComponents</a></li><li class="sidebar-sub-header"><a href="#router-resolve" class="sidebar-link">router.resolve</a></li><li class="sidebar-sub-header"><a href="#router-addroutes" class="sidebar-link">router.addRoutes</a></li><li class="sidebar-sub-header"><a href="#router-addroute" class="sidebar-link">router.addRoute</a></li><li class="sidebar-sub-header"><a href="#router-addroute-2" class="sidebar-link">router.addRoute</a></li><li class="sidebar-sub-header"><a href="#router-getroutes" class="sidebar-link">router.getRoutes</a></li><li class="sidebar-sub-header"><a href="#router-onready" class="sidebar-link">router.onReady</a></li><li class="sidebar-sub-header"><a href="#router-onerror" class="sidebar-link">router.onError</a></li></ul></li><li><a href="#路由对象" class="sidebar-link">路由对象</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#路由对象属性" class="sidebar-link">路由对象属性</a></li></ul></li><li><a href="#组件注入" class="sidebar-link">组件注入</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="#注入的属性" class="sidebar-link">注入的属性</a></li><li class="sidebar-sub-header"><a href="#增加的组件配置选项" class="sidebar-link">增加的组件配置选项</a></li></ul></li></ul></section></li></ul> <div class="sponsors" data-v-84121532=""><span data-v-84121532="">Sponsors</span> <a href="javascript:;" target="_blank" rel="noopener" data-v-84121532=""><img src="https://posva-sponsors.pages.dev/logos/vuejobs.svg" alt="VueJobs" data-v-84121532=""></a></div></aside> <main class="page"><div class="carbon-ads" data-v-84121532=""></div> <div class="theme-default-content content__default"><h1 id="api-参考"><a href="#api-参考" class="header-anchor">#</a> API 参考</h1> <h2 id="router-link"><a href="#router-link" class="header-anchor">#</a> <code>&lt;router-link&gt;</code></h2> <p><code>&lt;router-link&gt;</code> 组件支持用户在具有路由功能的应用中 (点击) 导航。
通过 <code>to</code> 属性指定目标地址，默认渲染成带有正确链接的 <code>&lt;a&gt;</code> 标签，可以通过配置 <code>tag</code> 属性生成别的标签.。另外，当目标路由成功激活时，链接元素自动设置一个表示激活的 CSS 类名。</p> <p><code>&lt;router-link&gt;</code> 比起写死的 <code>&lt;a href=&quot;...&quot;&gt;</code> 会好一些，理由如下：</p> <ul><li>无论是 HTML5 history 模式还是 hash 模式，它的表现行为一致，所以，当你要切换路由模式，或者在 IE9 降级使用 hash 模式，无须作任何变动。</li> <li>在 HTML5 history 模式下，<code>router-link</code> 会守卫点击事件，让浏览器不再重新加载页面。</li> <li>当你在 HTML5 history 模式下使用 <code>base</code> 选项之后，所有的 <code>to</code> 属性都不需要写 (基路径) 了。</li></ul> <h3 id="v-slot-api-3-1-0-新增"><a href="#v-slot-api-3-1-0-新增" class="header-anchor">#</a> <code>v-slot</code> API (3.1.0 新增)</h3> <p><code>router-link</code> 通过一个<a href="javascript:;" target="_blank" rel="noopener noreferrer">作用域插槽<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>暴露底层的定制能力。这是一个更高阶的 API，主要面向库作者，但也可以为开发者提供便利，多数情况用在一个类似 <em>NavLink</em> 这样的自定义组件里。</p> <p><strong>在使用 <code>v-slot</code> API 时，需要向 <code>router-link</code> 传入一个单独的子元素</strong>。否则 <code>router-link</code> 将会把子元素包裹在一个 <code>span</code> 元素内。</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span>
  <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/about<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">custom</span>
  <span class="token attr-name">v-slot</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ href, route, navigate, isActive, isExactActive }<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>NavLink</span> <span class="token attr-name">:active</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>isActive<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>href<span class="token punctuation">&quot;</span></span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>navigate<span class="token punctuation">&quot;</span></span>
    <span class="token punctuation">&gt;</span></span>{{ route.fullPath }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>NavLink</span>
  <span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><ul><li><code>href</code>：解析后的 URL。将会作为一个 <code>a</code> 元素的 <code>href</code> attribute。</li> <li><code>route</code>：解析后的规范化的地址。</li> <li><code>navigate</code>：触发导航的函数。<strong>会在必要时自动阻止事件</strong>，和 <code>router-link</code> 同理。</li> <li><code>isActive</code>：如果需要应用<a href="#active-class">激活的 class</a> 则为 <code>true</code>。允许应用一个任意的 class。</li> <li><code>isExactActive</code>：如果需要应用<a href="#exact-active-class">精确激活的 class</a> 则为 <code>true</code>。允许应用一个任意的 class。</li></ul> <h3 id="示例-将激活的-class-应用在外层元素"><a href="#示例-将激活的-class-应用在外层元素" class="header-anchor">#</a> 示例：将激活的 class 应用在外层元素</h3> <p>有的时候我们可能想把激活的 class 应用到一个外部元素而不是 <code>&lt;a&gt;</code> 标签本身，这时你可以在一个 <code>router-link</code> 中包裹该元素并使用 <code>v-slot</code> property 来创建链接：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span>
  <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/foo<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">v-slot</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ href, route, navigate, isActive, isExactActive }<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">custom</span>
<span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span>
    <span class="token attr-name">:class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>[isActive &amp;&amp; <span class="token punctuation">'</span>router-link-active<span class="token punctuation">'</span>, isExactActive &amp;&amp; <span class="token punctuation">'</span>router-link-exact-active<span class="token punctuation">'</span>]<span class="token punctuation">&quot;</span></span>
  <span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">:href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>href<span class="token punctuation">&quot;</span></span> <span class="token attr-name">@click</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>navigate<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{ route.fullPath }}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><div class="custom-block tip"><p class="custom-block-title">提示</p> <p>如果你在 <code>&lt;a&gt;</code> 元素上添加一个 <code>target=&quot;_blank&quot;</code>，则 <code>@click=&quot;navigate&quot;</code> 处理器会被忽略。</p></div> <h2 id="router-link-props"><a href="#router-link-props" class="header-anchor">#</a> <code>&lt;router-link&gt;</code> Props</h2> <h3 id="to"><a href="#to" class="header-anchor">#</a> to</h3> <ul><li><p>类型: <code>string | Location</code></p></li> <li><p>required</p> <p>表示目标路由的链接。当被点击后，内部会立刻把 <code>to</code> 的值传到 <code>router.push()</code>，所以这个值可以是一个字符串或者是描述目标位置的对象。</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token comment">&lt;!-- 字符串 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
<span class="token comment">&lt;!-- 渲染结果 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>home<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!-- 使用 v-bind 的 JS 表达式 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name"><span class="token namespace">v-bind:</span>to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">'</span>home<span class="token punctuation">'</span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!-- 不写 v-bind 也可以，就像绑定别的属性一样 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">'</span>home<span class="token punctuation">'</span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!-- 同上 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ path: <span class="token punctuation">'</span>home<span class="token punctuation">'</span> }<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>Home<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!-- 命名的路由 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ name: <span class="token punctuation">'</span>user<span class="token punctuation">'</span>, params: { userId: 123 }}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>User<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!-- 带查询参数，下面的结果为 /register?plan=private --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ path: <span class="token punctuation">'</span>register<span class="token punctuation">'</span>, query: { plan: <span class="token punctuation">'</span>private<span class="token punctuation">'</span> }}<span class="token punctuation">&quot;</span></span>
  <span class="token punctuation">&gt;</span></span>Register<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span>
<span class="token punctuation">&gt;</span></span>
</code></pre></div></li></ul> <h3 id="replace"><a href="#replace" class="header-anchor">#</a> replace</h3> <ul><li><p>类型: <code>boolean</code></p></li> <li><p>默认值: <code>false</code></p> <p>设置 <code>replace</code> 属性的话，当点击时，会调用 <code>router.replace()</code> 而不是 <code>router.push()</code>，于是导航后不会留下 history 记录。</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ path: <span class="token punctuation">'</span>/abc<span class="token punctuation">'</span>}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">replace</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre></div></li></ul> <h3 id="append"><a href="#append" class="header-anchor">#</a> append</h3> <ul><li><p>类型: <code>boolean</code></p></li> <li><p>默认值: <code>false</code></p> <p>设置 <code>append</code> 属性后，则在当前 (相对) 路径前添加基路径。例如，我们从 <code>/a</code> 导航到一个相对路径 <code>b</code>，如果没有配置 <code>append</code>，则路径为 <code>/b</code>，如果配了，则为 <code>/a/b</code></p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{ path: <span class="token punctuation">'</span>relative/path<span class="token punctuation">'</span>}<span class="token punctuation">&quot;</span></span> <span class="token attr-name">append</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre></div></li></ul> <h3 id="tag"><a href="#tag" class="header-anchor">#</a> tag</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;a&quot;</code></p> <p>有时候想要 <code>&lt;router-link&gt;</code> 渲染成某种标签，例如 <code>&lt;li&gt;</code>。
于是我们使用 <code>tag</code> prop 类指定何种标签，同样它还是会监听点击，触发导航。</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/foo<span class="token punctuation">&quot;</span></span> <span class="token attr-name">tag</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>li<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>foo<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
<span class="token comment">&lt;!-- 渲染结果 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span><span class="token punctuation">&gt;</span></span>foo<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
</code></pre></div></li></ul> <h3 id="active-class"><a href="#active-class" class="header-anchor">#</a> active-class</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;router-link-active&quot;</code></p> <p>设置链接激活时使用的 CSS 类名。默认值可以通过路由的构造选项 <code>linkActiveClass</code> 来全局配置。</p></li></ul> <h3 id="exact"><a href="#exact" class="header-anchor">#</a> exact</h3> <ul><li><p>类型: <code>boolean</code></p></li> <li><p>默认值: <code>false</code></p> <p>“是否激活”默认类名的依据是<strong>包含匹配</strong>。
举个例子，如果当前的路径是 <code>/a</code> 开头的，那么 <code>&lt;router-link to=&quot;/a&quot;&gt;</code> 也会被设置 CSS 类名。</p> <p>按照这个规则，每个路由都会激活 <code>&lt;router-link to=&quot;/&quot;&gt;</code>！想要链接使用“精确匹配模式”，则使用 <code>exact</code> 属性：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token comment">&lt;!-- 这个链接只会在地址为 / 的时候被激活 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/<span class="token punctuation">&quot;</span></span> <span class="token attr-name">exact</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><p>查看更多关于激活链接类名的例子<a href="javascript:;" target="_blank" rel="noopener noreferrer">可运行<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></li></ul> <h3 id="event"><a href="#event" class="header-anchor">#</a> event</h3> <ul><li><p>类型: <code>string | Array&lt;string&gt;</code></p></li> <li><p>默认值: <code>'click'</code></p> <p>声明可以用来触发导航的事件。可以是一个字符串或是一个包含字符串的数组。</p></li></ul> <h3 id="exact-active-class"><a href="#exact-active-class" class="header-anchor">#</a> exact-active-class</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;router-link-exact-active&quot;</code></p> <p>配置当链接被精确匹配的时候应该激活的 class。注意默认值也是可以通过路由构造函数选项 <code>linkExactActiveClass</code> 进行全局配置的。</p></li></ul> <h3 id="aria-current-value"><a href="#aria-current-value" class="header-anchor">#</a> aria-current-value</h3> <ul><li><p>类型: <code>'page' | 'step' | 'location' | 'date' | 'time' | 'true' | 'false'</code></p></li> <li><p>默认值: <code>&quot;page&quot;</code></p> <p>当链接根据精确匹配规则激活时配置的 <code>aria-current</code> 的值。这个值应该是 ARIA 规范中<a href="javascript:;" target="_blank" rel="noopener noreferrer">允许的 aria-current 的值<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewbox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>。在绝大多数场景下，默认值 <code>page</code> 应该是最合适的。</p></li></ul> <h2 id="router-view"><a href="#router-view" class="header-anchor">#</a> <code>&lt;router-view&gt;</code></h2> <p><code>&lt;router-view&gt;</code> 组件是一个 functional 组件，渲染路径匹配到的视图组件。<code>&lt;router-view&gt;</code> 渲染的组件还可以内嵌自己的 <code>&lt;router-view&gt;</code>，根据嵌套路径，渲染嵌套组件。</p> <p>其他属性 (非 router-view 使用的属性) 都直接传给渲染的组件，
很多时候，每个路由的数据都是包含在路由参数中。</p> <p>因为它也是个组件，所以可以配合 <code>&lt;transition&gt;</code> 和 <code>&lt;keep-alive&gt;</code> 使用。如果两个结合一起用，要确保在内层使用 <code>&lt;keep-alive&gt;</code>：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>transition</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>keep-alive</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>keep-alive</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>transition</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><h2 id="router-view-props"><a href="#router-view-props" class="header-anchor">#</a> <code>&lt;router-view&gt;</code> Props</h2> <h3 id="name"><a href="#name" class="header-anchor">#</a> name</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;default&quot;</code></p> <p>如果 <code>&lt;router-view&gt;</code>设置了名称，则会渲染对应的路由配置中 <code>components</code> 下的相应组件。查看 <a href="named-views2.html">命名视图</a> 中的例子。</p></li></ul> <h2 id="router-构建选项"><a href="#router-构建选项" class="header-anchor">#</a> Router 构建选项</h2> <h3 id="routes"><a href="#routes" class="header-anchor">#</a> routes</h3> <ul><li><p>类型: <code>Array&lt;RouteConfig&gt;</code></p> <p><code>RouteConfig</code> 的类型定义：</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">interface</span> <span class="token class-name">RouteConfig</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
  path<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span>
  component<span class="token operator">?</span><span class="token operator">:</span> Component<span class="token punctuation">,</span>
  name<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span> <span class="token comment">// 命名路由</span>
  components<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token punctuation">[</span>name<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">]</span><span class="token operator">:</span> Component <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token comment">// 命名视图组件</span>
  redirect<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">string</span> <span class="token operator">|</span> Location <span class="token operator">|</span> <span class="token builtin">Function</span><span class="token punctuation">,</span>
  props<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">boolean</span> <span class="token operator">|</span> Object <span class="token operator">|</span> <span class="token builtin">Function</span><span class="token punctuation">,</span>
  alias<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">string</span> <span class="token operator">|</span> <span class="token builtin">Array</span><span class="token operator">&lt;</span><span class="token builtin">string</span><span class="token operator">&gt;</span><span class="token punctuation">,</span>
  children<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">Array</span><span class="token operator">&lt;</span>RouteConfig<span class="token operator">&gt;</span><span class="token punctuation">,</span> <span class="token comment">// 嵌套路由</span>
  beforeEnter<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">(</span>to<span class="token operator">:</span> Route<span class="token punctuation">,</span> from<span class="token operator">:</span> Route<span class="token punctuation">,</span> next<span class="token operator">:</span> <span class="token builtin">Function</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">,</span>
  meta<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">any</span><span class="token punctuation">,</span>

  <span class="token comment">// 2.6.0+</span>
  caseSensitive<span class="token operator">?</span><span class="token operator">:</span> <span class="token builtin">boolean</span><span class="token punctuation">,</span> <span class="token comment">// 匹配规则是否大小写敏感？(默认值：false)</span>
  pathToRegexpOptions<span class="token operator">?</span><span class="token operator">:</span> Object <span class="token comment">// 编译正则的选项</span>
<span class="token punctuation">}</span>
</code></pre></div></li></ul> <h3 id="mode"><a href="#mode" class="header-anchor">#</a> mode</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;hash&quot; (浏览器环境) | &quot;abstract&quot; (Node.js 环境)</code></p></li> <li><p>可选值: <code>&quot;hash&quot; | &quot;history&quot; | &quot;abstract&quot;</code></p> <p>配置路由模式:</p> <ul><li><p><code>hash</code>: 使用 URL hash 值来作路由。支持所有浏览器，包括不支持 HTML5 History Api 的浏览器。</p></li> <li><p><code>history</code>: 依赖 HTML5 History API 和服务器配置。查看 <a href="history-mode2.html">HTML5 History 模式</a>。</p></li> <li><p><code>abstract</code>: 支持所有 JavaScript 运行环境，如 Node.js 服务器端。<strong>如果发现没有浏览器的 API，路由会自动强制进入这个模式。</strong></p></li></ul></li></ul> <h3 id="base"><a href="#base" class="header-anchor">#</a> base</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;/&quot;</code></p> <p>应用的基路径。例如，如果整个单页应用服务在 <code>/app/</code> 下，然后 <code>base</code> 就应该设为 <code>&quot;/app/&quot;</code>。</p></li></ul> <h3 id="linkactiveclass"><a href="#linkactiveclass" class="header-anchor">#</a> linkActiveClass</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;router-link-active&quot;</code></p> <p>全局配置 <code>&lt;router-link&gt;</code> 默认的激活的 class。参考 <a href="#router-link">router-link</a>。</p></li></ul> <h3 id="linkexactactiveclass"><a href="#linkexactactiveclass" class="header-anchor">#</a> linkExactActiveClass</h3> <ul><li><p>类型: <code>string</code></p></li> <li><p>默认值: <code>&quot;router-link-exact-active&quot;</code></p> <p>全局配置 <code>&lt;router-link&gt;</code> 默认的精确激活的 class。可同时翻阅 <a href="#router-link">router-link</a>。</p></li></ul> <h3 id="scrollbehavior"><a href="#scrollbehavior" class="header-anchor">#</a> scrollBehavior</h3> <ul><li><p>类型: <code>Function</code></p> <p>签名:</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token keyword">type</span> <span class="token class-name">PositionDescriptor</span> <span class="token operator">=</span>
  <span class="token punctuation">{</span> x<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> y<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token punctuation">}</span> <span class="token operator">|</span>
  <span class="token punctuation">{</span> selector<span class="token operator">:</span> <span class="token builtin">string</span> <span class="token punctuation">}</span> <span class="token operator">|</span>
  <span class="token keyword">void</span>

<span class="token keyword">type</span> <span class="token class-name">scrollBehaviorHandler</span> <span class="token operator">=</span> <span class="token punctuation">(</span>
  to<span class="token operator">:</span> Route<span class="token punctuation">,</span>
  from<span class="token operator">:</span> Route<span class="token punctuation">,</span>
  savedPosition<span class="token operator">?</span><span class="token operator">:</span> <span class="token punctuation">{</span> x<span class="token operator">:</span> <span class="token builtin">number</span><span class="token punctuation">,</span> y<span class="token operator">:</span> <span class="token builtin">number</span> <span class="token punctuation">}</span>
<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> PositionDescriptor <span class="token operator">|</span> <span class="token builtin">Promise</span><span class="token operator">&lt;</span>PositionDescriptor<span class="token operator">&gt;</span>
</code></pre></div><p>更多详情参考<a href="scroll-behavior2.html">滚动行为</a>。</p></li></ul> <h3 id="parsequery-stringifyquery"><a href="#parsequery-stringifyquery" class="header-anchor">#</a> parseQuery / stringifyQuery</h3> <ul><li><p>类型: <code>Function</code></p> <p>提供自定义查询字符串的解析/反解析函数。覆盖默认行为。</p></li></ul> <h3 id="fallback"><a href="#fallback" class="header-anchor">#</a> fallback</h3> <ul><li><p>类型: <code>boolean</code></p></li> <li><p>默认值: <code>true</code></p> <p>当浏览器不支持 <code>history.pushState</code> 控制路由是否应该回退到 <code>hash</code> 模式。默认值为 <code>true</code>。</p> <p>在 IE9 中，设置为 <code>false</code> 会使得每个 <code>router-link</code> 导航都触发整页刷新。它可用于工作在 IE9 下的服务端渲染应用，因为一个 hash 模式的 URL 并不支持服务端渲染。</p></li></ul> <h2 id="router-实例属性"><a href="#router-实例属性" class="header-anchor">#</a> Router 实例属性</h2> <h3 id="router-app"><a href="#router-app" class="header-anchor">#</a> router.app</h3> <ul><li><p>类型: <code>Vue instance</code></p> <p>配置了 <code>router</code> 的 Vue 根实例。</p></li></ul> <h3 id="router-mode"><a href="#router-mode" class="header-anchor">#</a> router.mode</h3> <ul><li><p>类型: <code>string</code></p> <p>路由使用的<a href="#mode">模式</a>。</p></li></ul> <h3 id="router-currentroute"><a href="#router-currentroute" class="header-anchor">#</a> router.currentRoute</h3> <ul><li><p>类型: <code>Route</code></p> <p>当前路由对应的<a href="#%E8%B7%AF%E7%94%B1%E5%AF%B9%E8%B1%A1">路由信息对象</a>。</p></li></ul> <h3 id="router-start-location"><a href="#router-start-location" class="header-anchor">#</a> router.START_LOCATION</h3> <ul><li><p>类型：<code>Route</code></p> <p>以<a href="#%E8%B7%AF%E7%94%B1%E5%AF%B9%E8%B1%A1">路由对象</a>的格式展示初始路由地址，即路由开始的地方。可用在导航守卫中以区分初始导航。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>

<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token comment">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> <span class="token keyword">from</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>from <span class="token operator">===</span> VueRouter<span class="token punctuation">.</span><span class="token constant">START_LOCATION</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 初始导航</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div></li></ul> <h2 id="router-实例方法"><a href="#router-实例方法" class="header-anchor">#</a> Router 实例方法</h2> <h3 id="router-beforeeach"><a href="#router-beforeeach" class="header-anchor">#</a> router.beforeEach</h3> <h3 id="router-beforeresolve"><a href="#router-beforeresolve" class="header-anchor">#</a> router.beforeResolve</h3> <h3 id="router-aftereach"><a href="#router-aftereach" class="header-anchor">#</a> router.afterEach</h3> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">/* 必须调用 `next` */</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

router<span class="token punctuation">.</span><span class="token function">beforeResolve</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">/* 必须调用 `next` */</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

router<span class="token punctuation">.</span><span class="token function">afterEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> <span class="token keyword">from</span></span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>增加全局的导航守卫。参考<a href="navigation-guards2.html">导航守卫</a>。</p> <p>在 2.5.0+ 这三个方法都返回一个移除已注册的守卫/钩子的函数。</p> <h3 id="router-push"><a href="#router-push" class="header-anchor">#</a> router.push</h3> <h3 id="router-replace"><a href="#router-replace" class="header-anchor">#</a> router.replace</h3> <h3 id="router-go"><a href="#router-go" class="header-anchor">#</a> router.go</h3> <h3 id="router-back"><a href="#router-back" class="header-anchor">#</a> router.back</h3> <h3 id="router-forward"><a href="#router-forward" class="header-anchor">#</a> router.forward</h3> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>location<span class="token punctuation">,</span> onComplete<span class="token operator">?</span><span class="token punctuation">,</span> onAbort<span class="token operator">?</span><span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>location<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>onComplete<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span>onAbort<span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>location<span class="token punctuation">,</span> onComplete<span class="token operator">?</span><span class="token punctuation">,</span> onAbort<span class="token operator">?</span><span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span>location<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>onComplete<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span>onAbort<span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">go</span><span class="token punctuation">(</span>n<span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
router<span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><p>动态的导航到一个新 URL。参考<a href="navigation2.html">编程式导航</a>。</p> <p>这些函数仅在安装路由插件并将其传递给 Vue 根实例后调用，如<a href="index10.html">起步</a>所示。</p> <h3 id="router-getmatchedcomponents"><a href="#router-getmatchedcomponents" class="header-anchor">#</a> router.getMatchedComponents</h3> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token literal-property property">matchedComponents</span><span class="token operator">:</span> Array<span class="token operator">&lt;</span>Component<span class="token operator">&gt;</span> <span class="token operator">=</span> router<span class="token punctuation">.</span><span class="token function">getMatchedComponents</span><span class="token punctuation">(</span>location<span class="token operator">?</span><span class="token punctuation">)</span>
</code></pre></div><p>返回目标位置或是当前路由匹配的组件数组 (是数组的定义/构造类，不是实例)。通常在服务端渲染的数据预加载时使用。</p> <h3 id="router-resolve"><a href="#router-resolve" class="header-anchor">#</a> router.resolve</h3> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token literal-property property">resolved</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">location</span><span class="token operator">:</span> Location<span class="token punctuation">;</span>
  <span class="token literal-property property">route</span><span class="token operator">:</span> Route<span class="token punctuation">;</span>
  <span class="token literal-property property">href</span><span class="token operator">:</span> string<span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token operator">=</span> router<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>location<span class="token punctuation">,</span> current<span class="token operator">?</span><span class="token punctuation">,</span> append<span class="token operator">?</span><span class="token punctuation">)</span>
</code></pre></div><p>解析目标位置 (格式和 <code>&lt;router-link&gt;</code> 的 <code>to</code> prop 一样)。</p> <ul><li><code>current</code> 是当前默认的路由 (通常你不需要改变它)</li> <li><code>append</code> 允许你在 <code>current</code> 路由上附加路径 (如同 <a href="#router-link.md-props"><code>router-link</code></a>)</li></ul> <h3 id="router-addroutes"><a href="#router-addroutes" class="header-anchor">#</a> router.addRoutes</h3> <p><em>已废弃</em>：使用 <a href="#router-addroute"><code>router.addRoute()</code></a> 代替。</p> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">addRoutes</span><span class="token punctuation">(</span>routes<span class="token operator">:</span> Array<span class="token operator">&lt;</span>RouteConfig<span class="token operator">&gt;</span><span class="token punctuation">)</span>
</code></pre></div><p>动态添加更多的路由规则。参数必须是一个符合 <code>routes</code> 选项要求的数组。</p> <h3 id="router-addroute"><a href="#router-addroute" class="header-anchor">#</a> router.addRoute</h3> <p>添加一条新路由规则。如果该路由规则有 <code>name</code>，并且已经存在一个与之相同的名字，则会覆盖它。</p> <p>函数签名:</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">addRoute</span><span class="token punctuation">(</span>route<span class="token operator">:</span> RouteConfig<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
</code></pre></div><h3 id="router-addroute-2"><a href="#router-addroute-2" class="header-anchor">#</a> router.addRoute</h3> <p>添加一条新的路由规则记录作为现有路由的子路由。如果该路由规则有 <code>name</code>，并且已经存在一个与之相同的名字，则会覆盖它。</p> <p>函数签名:</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">addRoute</span><span class="token punctuation">(</span>parentName<span class="token operator">:</span> <span class="token builtin">string</span><span class="token punctuation">,</span> route<span class="token operator">:</span> RouteConfig<span class="token punctuation">)</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span>
</code></pre></div><h3 id="router-getroutes"><a href="#router-getroutes" class="header-anchor">#</a> router.getRoutes</h3> <p>获取所有活跃的路由记录列表。<strong>注意只有文档中记录下来的 property 才被视为公共 API</strong>，避免使用任何其它 property，例如 <code>regex</code>，因为它在 Vue Router 4 中不存在。</p> <p>函数签名:</p> <div class="language-ts extra-class"><pre class="language-ts"><code><span class="token function">getRoutes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> RouteRecord<span class="token punctuation">[</span><span class="token punctuation">]</span>
</code></pre></div><h3 id="router-onready"><a href="#router-onready" class="header-anchor">#</a> router.onReady</h3> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">onReady</span><span class="token punctuation">(</span>callback<span class="token punctuation">,</span> <span class="token punctuation">[</span>errorCallback<span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre></div><p>该方法把一个回调排队，在路由完成初始导航时调用，这意味着它可以解析所有的异步进入钩子和路由初始化相关联的异步组件。</p> <p>这可以有效确保服务端渲染时服务端和客户端输出的一致。</p> <p>第二个参数 <code>errorCallback</code> 只在 2.4+ 支持。它会在初始化路由解析运行出错 (比如解析一个异步组件失败) 时被调用。</p> <h3 id="router-onerror"><a href="#router-onerror" class="header-anchor">#</a> router.onError</h3> <p>函数签名：</p> <div class="language-js extra-class"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">onError</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</code></pre></div><p>注册一个回调，该回调会在路由导航过程中出错时被调用。注意被调用的错误必须是下列情形中的一种：</p> <ul><li><p>错误在一个路由守卫函数中被同步抛出；</p></li> <li><p>错误在一个路由守卫函数中通过调用 <code>next(err)</code> 的方式异步捕获并处理；</p></li> <li><p>渲染一个路由的过程中，需要尝试解析一个异步组件时发生错误。</p></li></ul> <h2 id="路由对象"><a href="#路由对象" class="header-anchor">#</a> 路由对象</h2> <p>一个<strong>路由对象 (route object)</strong> 表示当前激活的路由的状态信息，包含了当前 URL 解析得到的信息，还有 URL 匹配到的<strong>路由记录 (route records)</strong>。</p> <p>路由对象是不可变 (immutable) 的，每次成功的导航后都会产生一个新的对象。</p> <p>路由对象出现在多个地方:</p> <ul><li><p>在组件内，即 <code>this.$route</code></p></li> <li><p>在 <code>$route</code> 观察者回调内</p></li> <li><p><code>router.match(location)</code> 的返回值</p></li> <li><p>导航守卫的参数：</p> <div class="language-js extra-class"><pre class="language-js"><code>router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// `to` 和 `from` 都是路由对象</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div></li> <li><p><code>scrollBehavior</code> 方法的参数:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token function">scrollBehavior</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> savedPosition</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// `to` 和 `from` 都是路由对象</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div></li></ul> <h3 id="路由对象属性"><a href="#路由对象属性" class="header-anchor">#</a> 路由对象属性</h3> <ul><li><p><strong>$route.path</strong></p> <ul><li><p>类型: <code>string</code></p> <p>字符串，对应当前路由的路径，总是解析为绝对路径，如 <code>&quot;/foo/bar&quot;</code>。</p></li></ul></li> <li><p><strong>$route.params</strong></p> <ul><li><p>类型: <code>Object</code></p> <p>一个 key/value 对象，包含了动态片段和全匹配片段，如果没有路由参数，就是一个空对象。</p></li></ul></li> <li><p><strong>$route.query</strong></p> <ul><li><p>类型: <code>Object</code></p> <p>一个 key/value 对象，表示 URL 查询参数。例如，对于路径 <code>/foo?user=1</code>，则有 <code>$route.query.user == 1</code>，如果没有查询参数，则是个空对象。</p></li></ul></li> <li><p><strong>$route.hash</strong></p> <ul><li><p>类型: <code>string</code></p> <p>当前路由的 hash 值 (带 <code>#</code>) ，如果没有 hash 值，则为空字符串。</p></li></ul></li> <li><p><strong>$route.fullPath</strong></p> <ul><li><p>类型: <code>string</code></p> <p>完成解析后的 URL，包含查询参数和 hash 的完整路径。</p></li></ul></li> <li><p><strong>$route.matched</strong></p> <ul><li>类型: <code>Array&lt;RouteRecord&gt;</code></li></ul> <p>一个数组，包含当前路由的所有嵌套路径片段的<strong>路由记录</strong> 。路由记录就是 <code>routes</code> 配置数组中的对象副本 (还有在 <code>children</code> 数组)。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  <span class="token literal-property property">routes</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token comment">// 下面的对象就是路由记录</span>
    <span class="token punctuation">{</span>
      <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'/foo'</span><span class="token punctuation">,</span>
      <span class="token literal-property property">component</span><span class="token operator">:</span> Foo<span class="token punctuation">,</span>
      <span class="token literal-property property">children</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token comment">// 这也是个路由记录</span>
        <span class="token punctuation">{</span> <span class="token literal-property property">path</span><span class="token operator">:</span> <span class="token string">'bar'</span><span class="token punctuation">,</span> <span class="token literal-property property">component</span><span class="token operator">:</span> Bar <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p>当 URL 为 <code>/foo/bar</code>，<code>$route.matched</code> 将会是一个包含从上到下的所有对象 (副本)。</p></li> <li><p><strong>$route.name</strong></p> <p>当前路由的名称，如果有的话。(查看<a href="named-routes2.html">命名路由</a>)</p></li> <li><p><strong>$route.redirectedFrom</strong></p> <p>如果存在重定向，即为重定向来源的路由的名字。(参阅<a href="redirect-and-alias2.html">重定向和别名</a>)</p></li></ul> <h2 id="组件注入"><a href="#组件注入" class="header-anchor">#</a> 组件注入</h2> <h3 id="注入的属性"><a href="#注入的属性" class="header-anchor">#</a> 注入的属性</h3> <p>通过在 Vue 根实例的 <code>router</code> 配置传入 router 实例，下面这些属性成员会被注入到每个子组件。</p> <ul><li><p><strong>this.$router</strong></p> <p>router 实例。</p></li> <li><p><strong>this.$route</strong></p> <p>当前激活的<a href="#%E8%B7%AF%E7%94%B1%E5%AF%B9%E8%B1%A1">路由信息对象</a>。这个属性是只读的，里面的属性是 immutable (不可变) 的，不过你可以 watch (监测变化) 它。</p></li></ul> <h3 id="增加的组件配置选项"><a href="#增加的组件配置选项" class="header-anchor">#</a> 增加的组件配置选项</h3> <ul><li><p><strong>beforeRouteEnter</strong></p></li> <li><p><strong>beforeRouteUpdate</strong></p></li> <li><p><strong>beforeRouteLeave</strong></p> <p>查看<a href="navigation-guards2.html#组件内的守卫">组件内的守卫</a>。</p></li></ul></div> <footer class="page-edit"><!----> <!----></footer> <!----> <div class="bsa-cpc-wrapper" data-v-84121532=""><div class="bsa-cpc"></div></div></main></div></div><div class="global-ui"><!----></div></div>
    <script src="static/js/app.5cb068d1.js" defer=""></script><script src="static/js/4.1430b5a5.js" defer=""></script><script src="static/js/3.51f41756.js" defer=""></script><script src="static/js/107.45f15a23.js" defer=""></script><script src="static/js/7.b1ef01fe.js" defer=""></script>
  </body>
</html>
